package com.tianzeculture.stagebao.activity.home.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.home.adapter.TalentAdapter;
import com.tianzeculture.stagebao.activity.home.popwindow.PrefessionTypePopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.SelectCityPopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.SelectPriceWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.TalentSearchPopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.TalentTypePopWindow;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.AllIndustryBean;
import com.tianzeculture.stagebao.bean.ArtistsBean;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.net.ArtistRestUsage;
import com.tianzeculture.stagebao.net.CommonRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tianzeculture.stagebao.util.Util.PAGE_SIZE;

/**
 * Created by Administrator on 2018/6/4 0004.
 * 人才库
 */

public class TalentFragment extends BaseFragment
        implements SelectCityPopWindow.SelectCityListner,
        PrefessionTypePopWindow.NoticeTypeListener
        , TalentSearchPopWindow.TalentSearchListener
        , SelectPriceWindow.SelectWindowLitenner {


    @BindView(R.id.rvTalent)
    RecyclerView rvTalent;

    @BindView(R.id.edtSearch)
    EditText edtSearch;
    TalentAdapter adapter = null;

    @BindView(R.id.boxCity)
    CheckBox boxCity;

    @BindView(R.id.boxMoney)
    CheckBox boxMoney;

    @BindView(R.id.boxStyle)
    CheckBox boxStyle;

    @BindView(R.id.boxSearch)
    CheckBox boxSearch;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Request
    ArtistRestUsage artistRestUsage = null;
    @Request
    CommonRestUsage commonRestUsage = null;
    private int toAlentpool = 101;
    private int industryListId = 102;//获取职业
    List<AllIndustryBean> allJobList;
    private int page = 1;
    String cityName = "";
    int userSex = 2;
    String searcher = "";
    String price = "";
    String age = "";
    String stature = "";
    String weight = "";
    String professio = "";
    List<User> newUsers = new ArrayList<>();

    //String adder, int userSex, String searcher, String price, String age, String stature, String weight, String professio, int page, int pageSize
    @Override
    public void initUI() {
        super.initUI();
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager
                (2, StaggeredGridLayoutManager.VERTICAL);
        rvTalent.setLayoutManager(layoutManager);
        rvTalent.setHasFixedSize(true);
        adapter = new TalentAdapter(getContext(), newUsers);
        rvTalent.setAdapter(adapter);
        commonRestUsage.industryList(industryListId);
        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    searcher = edtSearch.getText().toString().trim();
                    showProgressDialog();
                    page = 1;
                    artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
                }
                return false;
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
                        refreshlayout.finishRefresh();
//                        refreshlayout.setNoMoreData(false);
                    }
                }, 2000);

            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == industryListId) {
            if (msg.getData()) {
                if (msg.getObj() == null)
                    return;
                allJobList = ((ResultBean) msg.getObj()).getIndustry();
            }
        }
        if (taskId == toAlentpool) {
            ResultBean resultBean = (ResultBean) msg.getObj();
            List<User> myResult = resultBean.getMyResult();
            initImages(myResult);
        }
    }


    private void initImages(List<User> users) {
        if (page == 1)
            newUsers.clear();
        newUsers.addAll(users);
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.boxCity)
    public void showCityPopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }


        SelectCityPopWindow mPopWindow = new SelectCityPopWindow(getActivity(), cityName);
        mPopWindow.setSelectCityListener(this);
        mPopWindow.showPopupWindow(boxStyle);
    }

    @OnClick(R.id.boxStyle)
    public void showStylePopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }

        PrefessionTypePopWindow mPopWindow = new PrefessionTypePopWindow(getActivity(), allJobList);
        mPopWindow.setNoticeTypeListener(this);
        mPopWindow.showPopupWindow(boxStyle);
    }

    @OnClick(R.id.boxSearch)
    public void showSearchPopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }

        TalentSearchPopWindow mPopWindow = new TalentSearchPopWindow(getActivity());
        mPopWindow.setTalentSearchListener(this);
        mPopWindow.showPopupWindow(boxSearch);
    }

    @OnClick(R.id.boxMoney)
    public void showPricePopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }

        SelectPriceWindow priceWindow = new SelectPriceWindow(getActivity());
        priceWindow.setLitenner(this);
        priceWindow.showPopupWindow(boxMoney);
    }


    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_talent;
    }

    @Override
    public void pickSelectCityValue(String value) {
        boxCity.setText(value);
        if ("不限".equals(value))
            cityName = "";
        else
            cityName = value;
        page = 1;
        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
    }

    @Override
    public void selectCityCannel() {
        boxCity.setChecked(false);
    }

    @Override
    public void pickTalentSearchValue(String ag, String height, String weigh, int sex) {
        userSex = sex;
        this.age = ag;
        stature = height;
        this.weight = weigh;
        page = 1;
        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
    }

    @Override
    public void talentSearchCancel() {
        boxSearch.setChecked(false);
    }

    @Override
    public void pickNoticeTypeValue(String value) {
        boxStyle.setText(value);
        if ("不限".equals(value))
            professio = "";
        else
            professio = value;
        page = 1;
        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
    }

    @Override
    public void noticeTypeCancel() {
        boxStyle.setChecked(false);
    }

    @Override
    public void isPriceSelect(String str) {
        boxMoney.setText(str);
        if ("不限".equals(str)) {
            price = "";
        } else if ("200以下".equals(str)) {
            price = "-200";
        } else if ("50000以上".equals(str)) {
            price = "50000-";
        } else {
            price = str;
        }
        page = 1;
        artistRestUsage.toAlentpool(toAlentpool, cityName, userSex, searcher, price, age, stature, weight, professio, page, PAGE_SIZE);
    }

    @Override
    public void noticePriceCancel() {
        boxMoney.setChecked(false);
    }
}
